# encoding: utf-8
from apps.worker import worker
from apps.config import get_yaml_data
from apps.test_context import run
from apps.log import LOGGER
from apps.db import Sqlite3cls
from multiprocessing import Process, Manager
from apps import initdb
import os, sys, time
from apps.webserver import runserver
from apps import upload_agnet_info

rootdir = os.path.abspath(sys.path[0])

if __name__ == '__main__':
    # 初始化配置, 数据量
    configdata = get_yaml_data(rootdir)
    initdb(rootdir)
    upload_agnet_info(configdata)
    # time.sleep(1)

    LOGGER.info("启动主进程")

    with Manager() as mg:
        # 运行任务, 将全局配置加载到共享内存中
        global_context = mg.dict()
        global_context['cfg'] = configdata

        # 任务列表
        # task_list = mg.list()

        # 模拟生产者
        # p1 = Process(target=run, args=[global_context])
        # p1.start()

        """
        启动web
        """

        web = Process(target=runserver, )
        web.start()

        #  消费者
        p2 = Process(target=worker, args=[global_context])
        p2.start()

        p2.join()
